package com.ibeetl.admin.api.web;


import com.ibeetl.admin.api.entity.Travel_user;
import com.ibeetl.admin.api.service.UserService;
import com.ibeetl.admin.api.web.query.TravelUserQuery;
import com.ibeetl.admin.core.util.ConvertUtil;
import com.ibeetl.admin.core.util.ValidateConfig;
import com.ibeetl.admin.core.web.JsonResult;
import io.swagger.v3.oas.annotations.ExternalDocumentation;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.beetl.sql.core.page.PageResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


@RestController
@Tag(
        name = "ApiUserController")
public class ApiUserController {
    private final Log log = LogFactory.getLog(this.getClass());
    private static final String MODEL = "/api/user";

    @Resource
    private UserService userService;

    @Operation(
            summary = "登录",
            description = "根据手机号查询是否已注册，已注册就登录，无注册就先注册在登录"
    )
    @PostMapping(MODEL + "/login.json")
    @CrossOrigin
    public JsonResult<Travel_user> login(Travel_user apiUser) {
        long id = queryUserIdByPhone(apiUser.getPhone());
        if(id ==0){
            apiUser.setRegisterDt(new Date());
            return regist(apiUser);
        }
        apiUser.setUserId(id);

        return JsonResult.success(apiUser);
    }


    public JsonResult<Travel_user> regist(Travel_user apiUser) {
        boolean ok = userService.save(apiUser);
        if(ok){
            long id = queryUserIdByPhone(apiUser.getPhone());
            apiUser.setUserId(id);
            return JsonResult.success(apiUser);
        }
        return JsonResult.fail();

    }

    private long  queryUserIdByPhone(String phnoe) {
        return userService.queryUserIdByPhone(phnoe);
    }


    @PostMapping(MODEL + "/list.json")
    @ResponseBody
    public JsonResult<PageResult<Travel_user>> list(TravelUserQuery condition)
    {
        PageResult<Travel_user> pageResult = userService.queryByCondition(condition);
        return JsonResult.success(pageResult);
    }

    @PostMapping(MODEL + "/add.json")
    @ResponseBody
    public JsonResult add(@Validated(ValidateConfig.ADD.class)Travel_user travelUser)
    {
        userService.save(travelUser);
        return new JsonResult().success();
    }

    @PostMapping(MODEL + "/edit.json")
    @ResponseBody
    public JsonResult<String> update(@Validated(ValidateConfig.UPDATE.class)  Travel_user travelUser) {
        boolean success = userService.update(travelUser);
        if (success) {
            return new JsonResult().success();
        } else {
            return JsonResult.failMessage("保存失败");
        }
    }



    @GetMapping(MODEL + "/view.json")
    @ResponseBody
    public JsonResult<Travel_user>queryInfo(Integer userId) {
        Travel_user travelUser = userService.queryById( userId);
        return  JsonResult.success(travelUser);
    }

    @PostMapping(MODEL + "/delete.json")
    @ResponseBody
    public JsonResult delete(String ids) {
        if (ids.endsWith(",")) {
            ids = StringUtils.substringBeforeLast(ids, ",");
        }
        List<Long> idList = ConvertUtil.str2longs(ids);
        userService.batchDelTravelUser(idList);
        return new JsonResult().success();
    }

}
